﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace linkouqs.Gamekit {
    public class InventoryManager : MonoSingleton<InventoryManager> {
        private List<Item> itemList;
        BasePanel bpanel;
        #region CoinAmount
        public int coinAmount;
        public int CoinAmout {
            get {
                return coinAmount;
            }
            set {

                coinAmount = value;

            }
        }
        #endregion

        [HideInInspector]
        public Slot selectSlot;
        private bool isPickedItem = false;

        public bool IsPickedItem {
            get {
                return isPickedItem;
            }
        }

        private ItemUI pickedItem;
        public ItemUI PickedItem {
            get {
                return pickedItem;
            }
            set {
                pickedItem = value;
            }
        }


        private void Awake() {
            DontDestroyOnLoad(this.gameObject);
            ParseItemJson();

            Init();
        }

        private void Start() {
            pickedItem = GameObject.Find("UICanvas").transform.Find("PickedItem").GetComponent<ItemUI>();
        }

        void ParseItemJson() {
            itemList = new List<Item>();

            TextAsset itemText = Resources.Load<TextAsset>("Items");
            string itemJson = itemText.text;
            JSONObject j = new JSONObject(itemJson);

            foreach (JSONObject temp in j.list) {
                string typeStr = temp["ItemType"].str;
                ItemType type = (ItemType)System.Enum.Parse(typeof(ItemType), typeStr);

                int id = (int)temp["ID"].n;
                string name = temp["Name"].str;
                string description = temp["Description"].str;
                int capacity = (int)temp["Capacity"].n;
                int buyPrice = (int)temp["BuyPrice"].n;
                int sellPrice = (int)temp["SellPrice"].n;
                string sprite = temp["Sprite"].str;
                string goPath = temp["GoPath"].str;

                Item item = null;
                switch (type) {
                case ItemType.NullItemType:
                    Debug.LogWarning("物品  " + name + " 类型是:NullItemType");
                    break;
                case ItemType.ConsumableProp:
                    item = new ConsumeableProp(id, name, type, description, buyPrice, sellPrice, sprite, goPath, capacity);
                    break;
                case ItemType.UnConsumableProp:
                    item = new UnConsumeableProp(id, name, type, description, buyPrice, sellPrice, sprite, goPath, capacity);
                    break;
                case ItemType.Weapon:
                    int damage = (int)temp["Damage"].n;
                    int damageRange = (int)temp["DamageRange"].n;
                    item = new Weapon(id, name, type, description, buyPrice, sellPrice, sprite, goPath, capacity, damage, damageRange);
                    break;
                case ItemType.Drug:
                    int hp = (int)temp["HP"].n;
                    item = new Drug(id, name, type, description, buyPrice, sellPrice, sprite, goPath, capacity, hp);
                    break;
                default:
                    Debug.LogError("物品:" + name + " 没有类型");
                    break;
                }
                itemList.Add(item);

            }
        }

        public Item GetItemById(int id) {

            foreach (Item item in itemList) {
                if (item.ID == id) {
                    return item;
                }
            }
            return null;
        }


        public void ShowToolTip(string content) {

            bpanel = UIManager.Instance.panelStack.Peek();
            bpanel.transform.Find("ToolTip").GetComponent<ToolTip>().Show(content);
        }

        public void HideToolTip() {

            bpanel.transform.Find("ToolTip").GetComponent<ToolTip>().Hide();
        }

        public void PickupItem(Item item, Slot slot, int amount = 1) {
            pickedItem.SetItem(item, amount);

            ShowToolTip(item.Description);
            isPickedItem = true;
            this.selectSlot = slot;
            this.selectSlot.GetComponent<Image>().color = selectSlot.enterColor;

        }

        public void PickupItemUI(ItemUI item, Slot slot) {
            pickedItem.SetItemUI(item);

            ShowToolTip(item.Item.Description);
            isPickedItem = true;
            this.selectSlot = slot;
            this.selectSlot.GetComponent<Image>().color = selectSlot.enterColor;
        }

        public void RemoveItem(int amount = 1) {
            if (this.selectSlot == null) return;
            HideToolTip();
            isPickedItem = false;
            this.selectSlot.GetComponent<Image>().color = Color.white;
            this.selectSlot = null;

            if (pickedItem == null) return;

            pickedItem.ReduceAmount(amount);

        }


        public bool CosumeCoin(int amount) {
            if (CoinAmout >= amount) {
                CoinAmout -= amount;
                return true;
            }
            return false;
        }

        public void EarnCoin(int amount) {
            this.CoinAmout += amount;

        }

        void Init() {

            BasePanel knap = UIManager.Instance.GetPanel(UIPanelType.KnapsackPanel);

            knap.GetComponent<CanvasGroup>().alpha = 0;
            knap.GetComponent<CanvasGroup>().blocksRaycasts = false;

        }
    }
}